package com.jt.serviceImpl;

import com.jt.pojo.SearchResult;
import com.jt.service.SearchService;
import com.jt.util.HttpClientService;
import com.jt.utils.ObjectMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dc
 * @Classname SearchServiceImpl
 * @Date 2020/3/27 12:04
 */
@Service
@PropertySource("classpath:/properties/solr.properties")
public class SearchServiceImpl implements SearchService {

    @Autowired
    HttpClientService httpClientService;

    @Value("${search.base.url}")
    private String url;

    @Override
    public SearchResult findItemByKey(String q, int page, int rows) {
        Map<String, String> params = new HashMap<>();
        params.put("key",q);
        params.put("page",page+"");
        params.put("rows",rows+"");
        String json = httpClientService.doGet(url, params);
        SearchResult result = ObjectMapperUtil.toObject(json, SearchResult.class);
        return result;
    }
}
